//
//  AttributeStringCellData.m
//  18Birdies
//
//  Created by James Chen on 15/9/18.
//  Copyright © 2015年 Lc. All rights reserved.
//

#import "AttributeStringCell.h"
#import "AttributeStringCellData.h"
#import "LcFoundation.h"

@interface AttributeStringCellData () {
  CGFloat expectedCellHeight;
}

@end

@implementation AttributeStringCellData

+ (instancetype)cellDataWithAttributeString:(NSAttributedString*)string
                             onClickHandler:(OnCellClickedHanlder)onClickedHandler {
  AttributeStringCellData* cellData = [[self alloc] init];
  cellData->_describeString = string;
  cellData->_numberOfLines = 1;
  cellData.backgroundColor = LC_COLOR_WHITE;
  cellData.leftMargin = 15;
  if (onClickedHandler == nil) {
    cellData.clickable = NO;
  } else {
    cellData.clickable = YES;
    cellData.onCellClickedHandler = onClickedHandler;
  }
  cellData.cellHeight = 55;
  cellData.textAlignment = NSTextAlignmentLeft;
  cellData.expectedCellWidth = [UIScreen mainScreen].bounds.size.width;
  cellData.linkBreakMode = NSLineBreakByTruncatingMiddle;
  return cellData;
}

- (Class)cellClazz {
  return AttributeStringCell.class;
}

- (NSString*)nibName {
  return @"AttributeStringCell";
}

- (void)setCellHeight:(CGFloat)cellHeight {
  [super setCellHeight:cellHeight];
  expectedCellHeight = cellHeight;
}

- (void)refreshCell:(AttributeStringCell*)cell {
  [super refreshCell:cell];
  if (self.numberOfLines != 1) {
    cell.describeLabel.numberOfLines = self.numberOfLines;
    CGRect textRect = [self.describeString
        boundingRectWithSize:CGSizeMake(self.expectedCellWidth - 25, CGFLOAT_MAX)
                     options:NSStringDrawingUsesLineFragmentOrigin
                     context:nil];
    self.cellHeight =
        MAX((int)textRect.size.height + 15, expectedCellHeight ? expectedCellHeight : 55);
  } else {
    cell.describeLabel.numberOfLines = 1;
    self.cellHeight = expectedCellHeight ? expectedCellHeight : 55;
  }
  cell.backgroundColor = self.backgroundColor;
  cell.describeLabel.attributedText = self.describeString;
  cell.describeLabel.lineBreakMode = self.linkBreakMode;
  cell.textCenterYOffset.constant = self.textCenterYOffset;
  cell.leftMargin.constant = self.leftMargin;
  cell.rightMargin.constant = self.rightMargin;
  cell.describeLabel.textAlignment = self.textAlignment;
}

@end
